package com.kf.lib_network.http

import com.kf.lib_network.interceptor.SignInterceptor
import com.kf.lib_network.interceptor.TokenInterceptor
import com.kf.lib_network.retrofit.HttpType
import okhttp3.OkHttpClient

class HttpFactory {
    companion object {
        private val httpClientFactory = HttpClientFactory()

        val tokenOkhttp: OkHttpClient by lazy {
            httpClientFactory
                .createHttpBuilder()
                .addInterceptor(TokenInterceptor())
                .build()
        }

        val signOkhttp: OkHttpClient by lazy {
            httpClientFactory
                .createHttpBuilder()
                .addInterceptor(SignInterceptor())
                .build()
        }

        val tokenSignOkhttp: OkHttpClient by lazy {
            httpClientFactory
                .createHttpBuilder()
                .addInterceptor(TokenInterceptor())
                .addInterceptor(SignInterceptor())
                .build()
        }

        val noneOkhttp: OkHttpClient by lazy {
            httpClientFactory
                .createHttpBuilder()
                .build()
        }

    }

    fun createOkHttp(type: HttpType): OkHttpClient {
        lateinit var okhttp: OkHttpClient
        when (type) {
            HttpType.NONE -> {
                okhttp = noneOkhttp
            }
            HttpType.SIGN -> {
                okhttp = signOkhttp
            }
            HttpType.TOKEN -> {
                okhttp = tokenOkhttp
            }
            HttpType.TOKEN_SIGN -> {
                okhttp = tokenSignOkhttp
            }
        }
        return okhttp
    }

    private class HttpClientFactory : BaseHttp() {
        override fun connTime(): Long {
            return 30L
        }

        override fun writeTime(): Long {
            return 30L
        }

        override fun readTime(): Long {
            return 30L
        }

    }

}